<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>认证</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.connecting.html">链接服务器</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.connecting.rs.html">复制集合</a></div>
 <div class="up"><a href="mongo.connecting.html">链接服务器</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mongo.connecting.auth" class="section">
  <h2 class="title">认证</h2>
  <p class="para">
   如果 MongoDB 服务器启动时使用了 <em>--auth</em> 或
   <em>--keyFile</em> 参数，你就必须在进行任何操作前进行认证。
   你可以在连接时进行认证。方法是在链接字符串中指定用户名密码，或者在
   <span class="function"><a href="mongoclient.construct.html" class="function">MongoClient::__construct()</a></span> 构造函数中指定
   <em>&quot;username&quot;</em> 和 <em>&quot;password&quot;</em>。
  </p>
  <div class="example" id="mongo.connecting.auth-example">
   <p><strong>Example #1 针对“admin”数据库的认证</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Specifying&nbsp;the&nbsp;username&nbsp;and&nbsp;password&nbsp;in&nbsp;the&nbsp;connection&nbsp;URI&nbsp;(preferred)<br /></span><span style="color: #0000BB">$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">"mongodb://</span><span style="color: #007700">${</span><span style="color: #0000BB">username</span><span style="color: #007700">}</span><span style="color: #DD0000">:</span><span style="color: #007700">${</span><span style="color: #0000BB">password</span><span style="color: #007700">}</span><span style="color: #DD0000">@localhost"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Specifying&nbsp;the&nbsp;username&nbsp;and&nbsp;password&nbsp;via&nbsp;the&nbsp;options&nbsp;array&nbsp;(alternative)<br /></span><span style="color: #0000BB">$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">"mongodb://localhost"</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">"username"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$username</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"password"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$password</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
  <p class="para">
   默认状态下，驱动会针对 <em>admin</em> 数据库进行认证。认证也可以对其他数据库进行。方法是在连接字符串中指定数据库名，或者
   <span class="function"><a href="mongoclient.construct.html" class="function">MongoClient::__construct()</a></span> 构造函数中指定
   <em>&quot;db&quot;</em>。
  </p>
  <div class="example" id="mongo.connecting.auth-db-example">
   <p><strong>Example #2 针对一般数据库的认证</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Specifying&nbsp;the&nbsp;authentication&nbsp;database&nbsp;in&nbsp;the&nbsp;connection&nbsp;URI&nbsp;(preferred)<br /></span><span style="color: #0000BB">$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">"mongodb://</span><span style="color: #007700">${</span><span style="color: #0000BB">username</span><span style="color: #007700">}</span><span style="color: #DD0000">:</span><span style="color: #007700">${</span><span style="color: #0000BB">password</span><span style="color: #007700">}</span><span style="color: #DD0000">@localhost/myDatabase"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Specifying&nbsp;the&nbsp;authentication&nbsp;database&nbsp;via&nbsp;the&nbsp;options&nbsp;array&nbsp;(alternative)<br /></span><span style="color: #0000BB">$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">"mongodb://</span><span style="color: #007700">${</span><span style="color: #0000BB">username</span><span style="color: #007700">}</span><span style="color: #DD0000">:</span><span style="color: #007700">${</span><span style="color: #0000BB">password</span><span style="color: #007700">}</span><span style="color: #DD0000">@localhost"</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">"db"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"myDatabase"</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
  <p class="para">
   如果链接中断，驱动会重新尝试链接并会自动重新认证。
  </p>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.connecting.html">链接服务器</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.connecting.rs.html">复制集合</a></div>
 <div class="up"><a href="mongo.connecting.html">链接服务器</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
